package ee.ut.test;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import ee.ut.test.adapter.ResultListAdapter;
import ee.ut.test.download.ResultListAsyncTask;
import ee.ut.test.download.ResultListAsyncTask.ResultListDownloadListener;
import ee.ut.test.model.ResultListItem;

public class ResultListActivity extends Activity{

	public static final String STAFF_NAME_KEY = "staffName";
	private ProgressDialog progressDialog;
	private List<ResultListItem> resultList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_list);
        
        String searchString = getIntent().getExtras().getString(SearchActivity.SEARCH_STRING);
        TextView headerText = (TextView) findViewById(R.id.resultListHeader);
        headerText.setText(getResources().getString(R.string.result_list_header_prefix) + " " + searchString);
        
        progressDialog = ProgressDialog.show(this, null, getResources().getString(R.string.result_list_loading));

        // search type ette anda
        String searchType = getIntent().getExtras().getString(SearchActivity.SEARCH_TYPE);
        ResultListAsyncTask task = new ResultListAsyncTask(new ResultListDownloadListenerImpl());
        task.execute("fix.param");
    }
    
    protected final class ResultListDownloadListenerImpl implements ResultListDownloadListener {
		@Override
		public void onDownloadComplete(List<ResultListItem> items) {
			ListView listView = (ListView) findViewById(R.id.resultList);
			listView.setAdapter(new ResultListAdapter(ResultListActivity.this, items));
			resultList = items;
			listView.setOnItemClickListener(new OnItemClickListenerImpl());
			progressDialog.dismiss();
		}
    }
    
    private class OnItemClickListenerImpl implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
			Intent intent = new Intent(ResultListActivity.this, ScheduleTimeListActivity.class);
			intent.putExtra(STAFF_NAME_KEY, resultList.get(position).getFormattedFirstRow());
			startActivity(intent);
		}
    }
    
    
}
